<template>
	<view class="contenter">
		<!-- #ifdef MP-WEIXIN -->
		<view v-if="isCanUse">
			<view>
				<view class='header'>
					<image src='../../static/login_bg.jpg'></image>
				</view>
				<view class='content'>
					<view>申请获取以下权限</view>
					<text>第一步：获得你的公开信息(昵称，头像、地区等)</text>
				</view>
				<button class="bottom" hover-class="none" type='primary' @click="getUserProfile">
					登录授权
				</button>
			</view>
		</view>
		<view v-if="isCanUse2">
			<view>
				<view class='header'>
					<image :src='avatar'></image>
				</view>

				<view class='content'>
					<view>申请获取以下权限</view>
					<text>第二步：获得你的手机号用于绑定登录</text>
				</view>
				<button class='bottom' hover-class="none" type='primary' open-type="getPhoneNumber"
					@getphonenumber="PhoneNumber">
					授权手机登录
				</button>
			</view>
		</view>
		<!-- #endif -->
	</view>
</template>
<script>
	import {
		sendSmsCode
	} from '../../api/user/userApi.js'
	import context from '../../lib/java110/Java110Context.js';

	const constant = context.constant;
	// import {wechatSession,decodePhone,wechatLogin} from "@/api/user"
	export default {
		data() {
			return {
				code: "",
				rawData: '',
				SessionKey: '',
				openId: '',
				avatar: '',
				nick_name: "",
				avatarUrl: null,
				isCanUse: false, //默认为true
				isCanUse2: false, //默认为true
				session_key: '',
				openid: ''
			}
		},
		methods: {
			getUserProfile() {
				var that = this
				// #ifdef MP-WEIXIN
				uni.getProvider({
					service: 'oauth',
					success: function(res) {
						if (~res.provider.indexOf('weixin')) {
							uni.login({
								success: (res) => {
									that.code = res.code
									// that.wechatSession(res.code)
									console.log('获取信息code', res)
								}
							})
							uni.getUserProfile({
								desc: '登录',
								success: async (res) => {
									console.log("111", res)
									uni.setStorageSync('isCanUse', false)
									that.isCanUse = false
									that.isCanUse2 = true
									that.avatar = res.userInfo.avatarUrl
									that.nick_name = res.userInfo.nickName
									that.rawData = res.rawData
									// uni.setStorageSync('user', res)
								},
								fail: (res) => {
									console.log(res)
								}
							})

						} else {
							uni.showToast({
								title: '请先安装微信或升级版本',
								icon: "none"
							})
						}
					}
				})
				//#endif
			},
			//登录
			login() {
				let _this = this;
				uni.showLoading({
					title: '登录中...'
				})
			},
			//获取session_key
			// async wechatSession(code) {
			//     let that = this
			//     let data = await that.$http.post(`${wechatSession}`, {
			//         js_code: code,
			//         hotel_group_id: 1,
			//     })
			//     if (data) {
			//         console.log(data)
			//         that.session_key = data.data.session_key
			//         that.openid = data.data.openid
			//     }
			// },
			//解析手机号
			PhoneNumber(e) {
				let that = this
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					that.decodePhone(e)
				} else {
					console.log("用户点击了拒绝")
				}
			},
			//解密手机号码
			decodePhone(e) {

				let that = this
				console.log("解析手机号", e, that.code)
				uni.showLoading({
					title: '请稍后...',
					mask: false
				})
				uni.request({
					url: constant.url.userLogin, //演示地址，请以你的后端接口为准  
					method: 'POST',
					data: {
						code: that.code,
						// code:e.detail.code,
						rawData: that.rawData
					},

					success: (res) => {
						if (res.data.status == 0) {
							uni.hideLoading()
							console.log("res数据", res)
							uni.showToast({
								title: '授权成功',
								duration: 1500
							})

							uni.setStorageSync('token', res.data.token);
							uni.reLaunch({
								url: '/pages/index/index'
							})

						} else {
							uni.showToast({
								title: '网络异常',
								icon: 'none'
							})
						}
					}
				});



				//             let data = await that.$http.post(`${decodePhone}`, {
				//                 iv: iv,
				//                 encrypted: encryptedData,
				//                 session_key: session_key
				//             })
				// uni.showLoading({
				//     title: '请稍后...',
				//     mask: false
				// })
				// if (data.code == 0) {
				//     uni.hideLoading()
				//     that.wechatLogin(data.data.phone)
				//     that.phone = data.data.phone
				//     try {
				//         uni.setStorageSync("phone", that.phone)
				//     } catch {}

				// } else {
				//     uni.showToast({
				//         title: '网路异常',
				//         icon: 'none'
				//     })
				// }
			},
			// async wechatLogin(phone) {
			//     let that = this;
			//     let data = await this.$http.post(`${wechatLogin}`, {
			//         avator: that.avatar,
			//         hotel_group_id: 1,
			//         nickname: that.nick_name,
			//         openid: that.openid,
			//         phone: phone,
			//     })
			// console.log('-----wechatLogin-------', data)
			// if(data){
			//     uni.showToast({
			//         title:'授权成功',
			//         duration:1500
			//     })
			//     let loginData = data.data
			//     // console.log(loginData)
			//     uni.setStorageSync("loginUserInfo", loginData)
			//     uni.reLaunch({
			//         url:'/pages/my/index'
			//     })
			// }else{
			//     uni.showToast({
			//         title:'授权失败',
			//         icon:'none',
			//     })
			// }
			// },
		},
		onLoad() { //默认加载
			console.log(uni.getStorageSync('isCanUse'))
			if (uni.getStorageSync('access_token') != "") {
				uni.reLaunch({ //信息更新成功后跳转到小程序首页
					url: '/pages/index/index'
				})
			} else {
				this.isCanUse = true
			}
		}
	}
</script>

<style lang="scss" scoped>
	button[type=primary] {
		background: linear-gradient(90deg, #ff406d 17%, #ff7273 100%);
		color: #fff;
	}

	.header {
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 500rpx;
		margin: auto;
		line-height: 650rpx;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
		border-radius: 50%;

	}

	.content {
		margin-top: 50rpx;
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 180rpx 110rpx;
		font-size: 35rpx;
	}

	.contenter {
		width: 100%;
		height: calc(100vh - 0rpx);
		background-color: #191b1f;
	}
</style>
